package com.xwj.exception.solution_1.demo.controller;

import com.xwj.common.util.R;
import com.xwj.exception.common.exceptions.BusinessException;
import com.xwj.exception.solution_1.demo.enums.BusinessResponseEnum;
import com.xwj.exception.solution_1.demo.entity.User;
import com.xwj.exception.solution_1.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xiaowajiang
 * @Date: 2022-04-05 15:49
 * @Description:
 **/
@Slf4j
@RestController
@Api(value = "UserController", tags = "方案1 - 统一处理")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "POST请求 - 获取员工信息", position = 1)
    @PostMapping("solution_1/business/exception/getEmploye")
    public R getEmploye(@RequestParam Integer id) {
        User user = userService.selectById(id);
        if (user == null) {
            BusinessResponseEnum responseEnum = BusinessResponseEnum.USER_NOT_FOUND;
            throw new BusinessException(responseEnum.getCode(), responseEnum.getMessage());
        }
        return R.ok().put("data", user);
    }

    @ApiOperation(value = "GET请求 - 获取用户信息", position = 1)
    @GetMapping("solution_1/business/exception/getEmployeById")
    public R getEmployeById(@RequestParam Integer id) {
        User user = userService.selectById(id);
        if (user == null) {
            BusinessResponseEnum responseEnum = BusinessResponseEnum.USER_NOT_FOUND;
            throw new BusinessException(responseEnum.getCode(), responseEnum.getMessage());
        }
        return R.ok().put("data", user);
    }

}
